/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.harvest;

import cz.mzk.oai.metadataRepository.metadata.DateStamp;
import cz.mzk.oai.metadataRepository.repository.MetadataBase;
import java.io.File;

/**
 * This class represents file with exported records (daily updates or primary export) of base mzk03 from aleph with date of it's creation as DateStamp object
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class AlephExport implements Comparable {

    //format of file: mzk03-YYYYMMDD.m21
    private static String DATE_MASK = "YYYYMMDD";
    //private static String FILE_PREFIX = "mzk03-";
    private static String FILE_SUFFIX = ".m21";
    private File file;
    private DateStamp dateStamp;

    /**
     *
     * @param file 
     * @param base files with export from this base will be included in AlephExport
     * @throws IncorrectFilenameException if the name has incorrect form (possibly export from another base)
     */
    public AlephExport(File file, MetadataBase base) throws IncorrectFilenameException {
        this.file = file;
        String fileName = file.getName();
        String filePrefix = base.toString() + "-";
        if (fileName.length() != (filePrefix.length() + DATE_MASK.length() + FILE_SUFFIX.length())) {
            throw new IncorrectFilenameException(file.getAbsolutePath());
        }
        if (!fileName.substring(0, filePrefix.length()).equals(filePrefix)) {
            throw new IncorrectFilenameException(file.getAbsolutePath());
        }
        if (!fileName.substring(filePrefix.length() + DATE_MASK.length(), filePrefix.length() + DATE_MASK.length() + FILE_SUFFIX.length()).equals(FILE_SUFFIX)) {
            throw new IncorrectFilenameException(file.getAbsolutePath());
        }
        String year = fileName.substring(filePrefix.length(), filePrefix.length() + 4);
        String month = fileName.substring(filePrefix.length() + 4, filePrefix.length() + 6);
        String day = fileName.substring(filePrefix.length() + 6, filePrefix.length() + 8);
        dateStamp = new DateStamp(year, month, day);
    }

    /**
     * @return the file
     */
    public File getFile() {
        return file;
    }

    /**
     * @return the dateStamp of last change
     */
    public DateStamp getDateStamp() {
        return dateStamp;
    }

    public int compareTo(Object object) {
        if (object == null) {
            throw new NullPointerException();
        }
        if (object instanceof AlephExport) {
            AlephExport ex = (AlephExport) object;
            return dateStamp.compareTo(ex.getDateStamp());
        } else {
            throw new IllegalArgumentException("object must be an instance of AlephDailyUpdateExport");
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof AlephExport)) {
            return false;
        }
        return dateStamp.equals(((AlephExport) obj).getDateStamp());
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.dateStamp != null ? this.dateStamp.hashCode() : 0);
        return hash;
    }
}
